﻿/*
	Copyright (c) 2005 by Sean Kent. All rights reserved.
	Permission to use, copy, modify and distribute this software for any
	purpose is hereby granted without fee, provided this copyright and
	permissions notice appear in all copies and derivatives.
*/
#region Using directives

using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.Serialization;

#endregion

namespace Slobby.Common.NetworkFormatter {
	public class NetworkFormatter {
		public NetworkFormatter(Assembly sharedAssembly) {
			typeInformationStore = new AssemblyTypeInformation(sharedAssembly);
		}

		public void Serialize(Stream destinationStream, object graph) {
			BinaryWriter binarySerializationStream = new BinaryWriter(destinationStream);
			ObjectWriter writer = new ObjectWriter(typeInformationStore);
			writer.Serialize(binarySerializationStream, graph);
			binarySerializationStream.Flush();
		}

		public object Deserialize(Stream sourceStream) {
			BinaryReader binaryDeserializationStream = new BinaryReader(sourceStream);
			ObjectReader reader = new ObjectReader(typeInformationStore);
			object result = reader.Deserialize(binaryDeserializationStream);
			return result;
		}

		private AssemblyTypeInformation typeInformationStore;
	}
}
